package com.cn.wanxi.service;

import com.cn.wanxi.dto.NavDto;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.MyEnum;
import com.cn.wanxi.util.WriteRead;
import com.sun.deploy.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.cn.wanxi.util.MyPath.*;

public class NavService {


    /**
     * 封装list导航数据
     * 具体数据放在数组里面
     * 对象放在集合里面
     *
     * @return
     */
    public List<NavModel> getNavModelList() {
        /**
         * 1.存入记事本
         * 2.读取数据
         * 3.封装
         *
         */
        writeNote();
        String result = WriteRead.readNote(FILE_NAV);
        return setData(result);
    }

    private List<NavModel> setData(String result) {
        List<NavModel> navModelList = new ArrayList<>();
        String[] results = result.split(SPLIT);
        String href = results[0];
        String[] hrefs = href.split(",");
        String title = results[1];
        String[] titles = title.split(",");
        for (int i = 0; i < hrefs.length; i++) {
            NavModel model = new NavModel();//封装的具体对象
            //封装每一个具体对象然后加入list
            String tempHref = hrefs[i];
            tempHref = tempHref.replace("[", "");
            tempHref = tempHref.replace("]", "");
            model.setHref(tempHref);
            model.setTitle(titles[i]);
            navModelList.add(model);
        }
        return navModelList;
    }

    //    private String readNote() {
//        File file = new File(PATH, FILE_NAV);
//        if (!file.exists()) {
//            return "当前地址不存在";
//        }
//        StringBuilder stringBuilder = new StringBuilder();
//        try {
//            FileInputStream fileInputStream = new FileInputStream(file);//将文件放入输入流
////            将流强制转换为utf-8的编码
//            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
////           将转换后的流放入缓冲流
//            Reader reader = new BufferedReader(inputStreamReader);
//            int result = 0;
//            while ((result = reader.read()) != -1) {
//                stringBuilder.append((char) result);
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return stringBuilder.toString();
//    }
    public static void main(String[] args) {
        NavService navService = new NavService();
        navService.getNavModelList();
    }

    private void writeNote() {
        List<String> list = new ArrayList<>();
        NavDto navDto = new NavDto();
        list.add(Arrays.toString(navDto.hrefs));
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < navDto.titles.size(); i++) {
            if(i==(navDto.titles.size()-1)){
                stringBuilder.append(navDto.titles.get(i));
            }else {
                stringBuilder.append(navDto.titles.get(i)).append(",");
            }
        }
        list.add(stringBuilder.toString());
        WriteRead.writeNote(MyEnum.FILE_NAV.getPath(), list);
    }
}
